<?php
namespace Admin\Controller;
use Think\Controller;
class PageController extends BaseController {
    public function page_list(){
        $this->display();
    }
    public function page_ajax(){
        $limit=5;
        if(I('get.sou')){
            $page_name=I('get.sou');
            $count=M('page')->where("page_name like '%$page_name%'")->count();
        }else{
            $count=M('page')->count();
        }
        $page=new \Think\Page($count,$limit);
        $show=$page->show();
        if(I('get.sou')){
            $page_name=I('get.sou');
            $p=M('page')
                    ->where("page_name like '%$page_name%'")
                    ->order("page_id DESC")
                    ->limit($page->firstRow.','.$page->listRows)
                    ->select();
        }else{
            $p=M('page')
                    ->order("page_id DESC")
                    ->limit($page->firstRow.','.$page->listRows)
                    ->select();
        }
        $this->assign('show',$show);
        $this->assign('p',$p);
        $this->assign('a',$page->firstRow+1);
        $this->display();
    }

    public function page_add(){
    	if(I('post.')){
    		$data=array(
    			'page_name'=>I('post.page_name'),
                'page_content'=>I('post.page_content'),
    		);
            if($_FILES['page_img']['size']){
                $upload=new \Think\Upload();
                $upload->exts=array('jpg','png','gif');
                $upload->rootPath='./Upload/';
                $upload->savePath='page/';
                $info=$upload->upload();
                if($info){
                    $data['page_img']=$info['page_img']['savepath'].$info['page_img']['savename'];
                }
            }
    		$res=M('page')->add($data);
    		if($res){
    			$this->success('添加成功',U('Admin/page/page_list'));
    			exit();
    		}else{
    			$this->error('数据有误，请重新提交');
    		}
    	}
    	$this->display();
    }
    public function page_edit(){
    	$aid=I('get.aid');
    	$page=M('page')->where("page_id=$aid")->find();
    	$this->assign('page',$page);
    	if(I('post.')){
    		$data=array(
    			'page_name'=>I('post.page_name'),
                'page_content'=>I('post.page_content'),
    		);
            $page_id=I('post.page_id');
            if($_FILES['page_img']['size']){
                $oldarr=M('page')->field("page_img")->where("page_id=$page_id")->find();
                $path='./Upload/'.$oldarr['page_img'];
                if(!empty($oldarr) && file_exists($path)){
                    unlink($path);
                }
                $upload=new \Think\Upload();
                $upload->exts=array('jpg','png','gif');
                $upload->rootPath='./Upload/';
                $upload->savePath='page/';
                $info=$upload->upload();
                if($info){
                    $data['page_img']=$info['page_img']['savepath'].$info['page_img']['savename'];
                }
            }
    		$res=M('page')->where("page_id=$page_id")->save($data);
    		if($res){
    			$this->success('编辑成功',U('Admin/page/page_list'));
    			exit();
    		}else{
    			$this->error('数据有误，请重新提交');
    			exit();
    		}
    	}
    	$this->display();
    }
    public function page_del(){
    	$a_id=I('get.a_id');
        if($_FILES['page_img']['size']){
                $oldarr=M('page')->field("page_img")->where("page_id=$a_id")->find();
                $path='./Upload/'.$oldarr['page_img'];
                if(!empty($oldarr) && file_exists($path)){
                    unlink($path);
                }
        }
		$res=M('page')->where("page_id=$a_id")->delete();
		if($res){
			echo 1;exit();
		}else{
			echo M('page')->getLastSql();exit();
		}
    }
    public function name_ajax(){
        if(!empty($_GET['value'])){
            $id=$_GET['id'];
             $value=$_GET['value'];
             $data=array(
                 'page_name'=>$_GET['value'],
             );
            $res=M('page')->where("page_id=$id")->save($data);
            if($res){
                 echo 1;exit();
            }else{
               echo 2;exit();
           }
       }
    }
}